﻿using Crawler.WebEntities;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;

namespace Crawler.DataHelper
{
    public static class WebHelper
    {
        #region 私有变量
        /// <summary>
        /// 网页URL地址
        /// </summary>
        private static string url = null;
        /// <summary>
        /// 是否使用代码服务器：0 不使用  1 使用代理服务器
        /// </summary>
        private static int proxyState = 0;
        /// <summary>
        /// 代理服务器地址
        /// </summary>
        private static string proxyAddress = "proxy2.nfdaily.com";
        /// <summary>
        /// 代理服务器端口
        /// </summary>
        private static string proxyPort = "8080";
        /// <summary>
        /// 代理服务器用户名
        /// </summary>
        private static string proxyAccount = "zhangchi";
        /// <summary>
        /// 代理服务器密码
        /// </summary>
        private static string proxyPassword = "123789";
        /// <summary>
        /// 代理服务器域
        /// </summary>
        private static string proxyDomain = "nfdaily";

        #endregion



        #region 公共方法
        /// <summary>
        /// 读取指定URL地址，获取内容
        /// </summary>

        public static string GetContent(MyUri uri)
        {
            string content = null;
            //HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri.AbsoluteUri);
            int time = 0;

            //WebProxy myProxy = new WebProxy();
            //myProxy = (WebProxy)request.Proxy;
            //myProxy.Address = new Uri(proxyAddress + ":" + proxyPort);
            //myProxy.Credentials = new NetworkCredential(proxyAccount, proxyPassword, proxyDomain);
            //request.Proxy = myProxy;

            WebRequest request = WebRequest.Create(uri.AbsoluteUri);
            ////使用代理服务器的处理
            //WebProxy myProxy = new WebProxy();
            //myProxy = (WebProxy)request.Proxy;
            //myProxy.Address = new Uri(proxyAddress + ":" + proxyPort);
            //myProxy.Credentials = new NetworkCredential(proxyAccount, proxyPassword, proxyDomain);
            //request.Proxy = myProxy;
            try
            {
                //请求服务
                WebResponse response = request.GetResponse();
                //返回信息
                Stream resStream = response.GetResponseStream();
                StreamReader sr = new StreamReader(resStream, System.Text.Encoding.Default);
                string tempCode = sr.ReadToEnd();
                resStream.Close();
                sr.Close();
                content = tempCode;
            }
            catch
            {

            }
            return content;
        }
        #endregion

    }
}
